package gui.panels;

import gui.abstracts.AbstractPanel;
import gui.abstracts.AbstractTable;
import gui.containerDate.Item;
import gui.tables.TableStudent;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import bean.StudentBean;
import bean.UniversityBean;

import commandPatern.Insert;
import commandPatern.StoreCommands;
import commandPatern.Update;

import dao.GroupDAO;
import dao.StudentDAO;
import dao.UniversityDAO;

import patern.Opservator;

public class PanelStudent extends AbstractPanel implements Opservator {

	/**
	 * 
	 */
	private JComboBox comboBox;
	
	private Vector<Item> model;

	private Insert insert;

	private JScrollPane scrollTableStudent;

	private Vector<Integer> editRowColomn = new Vector<Integer>();

	private static PanelStudent panelStudent = null;
	
	private int preferredSizeWidth = 180;
	private int preferredSizeHeight = 30;
	
	private int maximumSizeWidth = 250;
	private int maximumSizeHeight = 30;

	private PanelStudent() {
		initGui();
	}

	public static PanelStudent getInstance() {
		if (panelStudent == null) {
			panelStudent = new PanelStudent();
		}
		return panelStudent;
	}

	public void initGui() {

		model = new Vector<Item>();
		BoxLayout layoutStudent = new BoxLayout(this, BoxLayout.Y_AXIS);
		this.setLayout(layoutStudent);

		comboBox = new JComboBox(model);
		comboBox.setPreferredSize(new Dimension(preferredSizeWidth, preferredSizeHeight));
		comboBox.setMaximumSize(new Dimension(maximumSizeWidth, maximumSizeHeight));
		this.add(comboBox);

		jAbstractTable = new TableStudent();

		scrollTableStudent = new JScrollPane(jAbstractTable);

		this.add(scrollTableStudent);
	}

	public void setComboBox(JComboBox comboBox) {
		this.comboBox = comboBox;
	}

	public AbstractTable getTable() {
		return  jAbstractTable;
	}

	public JComboBox getComboBox() {
		return comboBox;
	}

	public Vector<Item> getComboBoxModel() {
		return this.model;
	}

	public void setStudentTable(Item item) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {

		int count = 0;
		((TableStudent) jAbstractTable).deleteAllDataFromTable();

		DefaultTableModel jTableModelStudent = ((TableStudent) jAbstractTable)
				.getDefaultTableMOdel();

		StudentDAO StDAO = new StudentDAO();

		ArrayList<StudentBean> students = StDAO.getArrayListStudents();

		for (StudentBean studentBean : students) {
			if (studentBean.getIdGr() == item.getId()) {
				jTableModelStudent.insertRow(count, studentBean.getRow());
				count++;
			}
		}
	}

	public void initTable() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		StudentDAO StDAO = new StudentDAO();
		ArrayList<StudentBean> students = StDAO.InitStudents();
	}

	public UniversityBean getUniversityBean(int i) {

		UniversityBean univDAO = null;
		Item item = (Item) comboBox.getSelectedItem();
		if (i == -1) {

			i = jAbstractTable.getRowCount();
			
			String nume = (String) jAbstractTable.getValueAt(i - 1,1);
			String prenume = (String) jAbstractTable.getValueAt(i - 1, 2);
			String telephone = (String) jAbstractTable.getValueAt(i - 1, 3);
			int groupId = item.getId();
			
			univDAO = new StudentBean(nume , prenume,telephone, groupId,0);
		} else {
			
			String nume = (String) jAbstractTable.getValueAt(i, 1);
			String prenume = (String) jAbstractTable.getValueAt(i, 2);
			String telephone = (String) jAbstractTable.getValueAt(i, 3);
			int groupId = item.getId();
			int studentId = Integer.parseInt((String) jAbstractTable.getValueAt(i, 0));
			
			univDAO = new StudentBean(nume, prenume, telephone, groupId, studentId);
		}

		return univDAO;
	}

	@Override
	public void add(Item item) {

		model.add(item);

	}

	@Override
	public void remove(Item item) {
		System.out.println("treb sa se stearga");
		int i;

		for (i = 0; i < model.size(); i++) {
			System.out.println(i + " " + model.get(i).getId() + " "
					+ item.getId());
			if (model.get(i).getId() == item.getId()) {
				model.remove(i);

			}
		}
	}







	@Override
	public void cancelInsert() {
		// TODO Auto-generated method stub
		
	}

}
